package com.yoomiito.app.widget.dialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.text.Html;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yoomiito.app.R;
import com.yoomiito.app.adapter.PickGoodsAdapter;
import com.yoomiito.app.model.bean.AddressInfo;
import com.yoomiito.app.model.bean.GoodsDetail;
import com.yoomiito.app.model.bean.GoodsList;
import com.yoomiito.app.utils.DensityUtil;
import com.yoomiito.app.utils.ImageLoader;
import com.yoomiito.app.widget.NumControlView;
import com.yoomiito.app.widget.OnNumberChangeListener;

import java.util.List;

public class PickDialog extends BaseDialog implements View.OnClickListener {

    private TextView tvAddress;
    private TextView tvPhone;
    private TextView tvName;
    private TextView mCancelTv;
    private TextView tvRemark;
    private OnClickListener mOnClickListener;
    private View mLineTv;
    private boolean flag = false;
    private AddressInfo mAddressInfo;
    private RecyclerView rvGood;
    private List<GoodsList> goodsLists;

    public String remark;

    public PickDialog(@NonNull Context context) {
        this(context, 0);
    }

    public PickDialog(@NonNull Context context, int themeResId) {
        super(context, R.style.dialog_2);
    }

    protected PickDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @SuppressLint("MissingInflatedId")
    @Override
    public View setView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_pick, null);
        tvName = view.findViewById(R.id.tv_name);
        tvPhone = view.findViewById(R.id.tv_phone);
        tvAddress = view.findViewById(R.id.tv_address);
        mCancelTv = view.findViewById(R.id.item_cancel);
        mLineTv = view.findViewById(R.id.item_line);
        rvGood = view.findViewById(R.id.rv_good);
        tvRemark = view.findViewById(R.id.tv_remark);
        rvGood.setLayoutManager(new LinearLayoutManager(mContext));
        mCancelTv.setOnClickListener(this);
        setData();
        view.findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        view.findViewById(R.id.item_sure).setOnClickListener(this);
        setWidthPercent(0.85);
        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.item_cancel:
                if (mOnClickListener != null) {
                    mOnClickListener.onClick(false);
                }
                dismiss();
                break;
            case R.id.item_sure:
                if (mOnClickListener != null) {
                    mOnClickListener.onClick(true);
                }
                break;
        }
    }


    public void setAddress(AddressInfo mAddressInfo, List<GoodsList> goodsLists, String remark) {
        this.mAddressInfo = mAddressInfo;
        this.goodsLists = goodsLists;
        this.remark = remark;
        setData();
    }


    private void setData() {
        if (mAddressInfo != null && tvName != null && tvPhone != null && tvAddress != null) {
            tvName.setText(mAddressInfo.getUserName());
            tvPhone.setText(mAddressInfo.getMobile());
            tvAddress.setText(mAddressInfo.getProvinceName() + " "
                    + mAddressInfo.getCityName() + " " + mAddressInfo.getCountyName() + " " + mAddressInfo.getAddress());
        }
        if (tvRemark != null) {
            if (TextUtils.isEmpty(remark)) {
                tvRemark.setText("-");
            } else {
                tvRemark.setText(remark);
            }
        }


        if (rvGood != null && goodsLists != null && goodsLists.size() > 0) {
            GoodsAdapter adapter = new GoodsAdapter();
            adapter.setNewData(goodsLists);
            rvGood.setAdapter(adapter);
        }
    }

    public PickDialog setOnClickListener(OnClickListener onClickListener) {
        mOnClickListener = onClickListener;
        return this;

    }

    static class GoodsAdapter extends BaseQuickAdapter<GoodsList, BaseViewHolder> {

        public GoodsAdapter() {
            super(R.layout.item_pick_dialog);
        }

        @Override
        protected void convert(BaseViewHolder helper, GoodsList item) {

            helper.setText(R.id.tv_name, item.productName);
            helper.setText(R.id.tv_num, "" + item.num + item.unit);
        }
    }

    @Override
    public void onBackPressed() {
        if (!flag) {
            super.onBackPressed();
        }
    }

    public interface OnClickListener {
        void onClick(boolean isSure);
    }
}
